
package com.jf.cloud.supplier.controller.admin;

import com.jf.cloud.api.supplier.vo.SupplierApiRefundAddrVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.supplier.dto.SupplierRefundAddrDTO;
import com.jf.cloud.supplier.model.SupplierRefundAddr;
import com.jf.cloud.supplier.service.SupplierRefundAddrService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 退货地址
 *
 * @author zz
 * @date 2021-01-27 09:10:00
 */
@RestController("adminSupplierRefundAddrController")
@RequestMapping("/mp/supplier_refund_addr")
@Tag(name = "admin-供应商店铺退货地址")
public class SupplierRefundAddrController {

    @Autowired
    private SupplierRefundAddrService supplierRefundAddrService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "分页获取退货地址列表" , description = "分页获取退货地址列表")
    public ServerResponseEntity<PageVO<SupplierApiRefundAddrVO>> page(@Valid PageDTO pageDTO, @ParameterObject SupplierRefundAddrDTO supplierRefundAddrDTO) {
        supplierRefundAddrDTO.setSupplierId(AuthUserContext.get().getTenantId());
        PageVO<SupplierApiRefundAddrVO> refundAddrPage = supplierRefundAddrService.page(pageDTO, supplierRefundAddrDTO);
        return ServerResponseEntity.success(refundAddrPage);
    }

    @GetMapping
    @Operation(summary = "获取退货地址" , description = "根据supplierRefundAddrId获取退货地址")
    public ServerResponseEntity<SupplierApiRefundAddrVO> getBySupplierRefundAddrId(@RequestParam Long supplierRefundAddrId) {
        Long supplierId = AuthUserContext.get().getTenantId();
        SupplierApiRefundAddrVO bySupplierRefund = supplierRefundAddrService.getBySupplierRefundAddrId(supplierRefundAddrId, supplierId);
        return ServerResponseEntity.success(bySupplierRefund);
    }

    @PostMapping
    @Operation(summary = "保存退货地址" , description = "保存退货地址")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SupplierRefundAddrDTO supplierRefundAddrDTO) {
        SupplierRefundAddr supplierRefundAddr = mapperFacade.map(supplierRefundAddrDTO, SupplierRefundAddr.class);
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierRefundAddr.setSupplierId(supplierId);
        supplierRefundAddrService.save(supplierRefundAddr);
        supplierRefundAddrService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新退货地址" , description = "更新退货地址")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SupplierRefundAddrDTO supplierRefundAddrDTO) {
        SupplierRefundAddr supplierRefundAddr = mapperFacade.map(supplierRefundAddrDTO, SupplierRefundAddr.class);
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierRefundAddr.setSupplierId(AuthUserContext.get().getTenantId());
        supplierRefundAddrService.update(supplierRefundAddr);
        supplierRefundAddrService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除退货地址" , description = "根据退货地址id删除退货地址")
    public ServerResponseEntity<Void> delete(@RequestParam Long supplierRefundAddrId) {
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierRefundAddrService.deleteById(supplierRefundAddrId, supplierId);
        supplierRefundAddrService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }
    @GetMapping("/list")
    @Operation(summary = "获取退货地址列表" , description = "获取退货地址列表")
    public ServerResponseEntity<List<SupplierApiRefundAddrVO>> list() {
        return ServerResponseEntity.success(supplierRefundAddrService.listBySupplierId(AuthUserContext.get().getTenantId()));
    }
}
